#include "device.h"
#include "modbus.h"
#include <QDebug>
Device::Device()
{
    this->moveToThread(&thread);
    thread.start();
    connect(this,SIGNAL(Signal_ToNewDevice()),this,SLOT(Slots_ToNewDevice()),Qt::BlockingQueuedConnection);
    emit Signal_ToNewDevice();
    modbus->timeout=200;
    SlotsInit();
}

Device::~Device()
{
    thread.quit();
    thread.wait();
}

void Device::OpenDevice(QString portName,int baudrate)
{
    qDebug()<<__FUNCTION__<<__LINE__<<portName<<baudrate;
    emit Signal_ToOpenDevice(portName,baudrate);
    qDebug()<<__FUNCTION__<<__LINE__;
}

void Device::Channel1InputFullCalibration(int value)
{
    short sendValue;
    sendValue=value;
    SendFc6(1,1002,1,&sendValue);
}

void Device::Channel2InputFullCalibration(int value)
{
    short sendValue;
    sendValue=value;
    SendFc6(1,1102,1,&sendValue);
}

void Device::Channel1ZeroAdjustment(short value)
{
    short sendValue;
    sendValue=value;
    SendFc6(1,1201,1,&sendValue);

}

void Device::Channel1FullAdjustment(short value)
{
    short sendValue;
    sendValue=value;
    SendFc6(1,1202,1,&sendValue);
}

void Device::Channel2ZeroAdjustment(short value)
{
    short sendValue;
    sendValue=value;
    SendFc6(1,1301,1,&sendValue);
}

void Device::Channel2FullAdjustment(short value)
{
    short sendValue;
    sendValue=value;
    SendFc6(1,1302,1,&sendValue);
}

void Device::SaveChannel1()
{
    short sendValue;
    sendValue=1;
    SendFc6(1,1203,1,&sendValue);
}

void Device::SaveChannel2()
{
    short sendValue;
    sendValue=1;
    SendFc6(1,1303,1,&sendValue);
}

void Device::SetChannel1Range(int value)
{
    short sendValue;
    sendValue=value;
    SendFc6(1,2000,1,&sendValue);
}

void Device::SetChannel2Range(int value)
{
    short sendValue;
    sendValue=value;
    SendFc6(1,2001,1,&sendValue);
}

void Device::SetDeviceType(int value)
{
    short sendValue;
    sendValue=value;
    SendFc6(1,4000,1,&sendValue);
}

void Device::Channel1ZeroCalibration()
{
    short sendValue;
    sendValue=0;
    SendFc6(1,1001,1,&sendValue);
}

void Device::Channel2ZeroCalibration()
{
    short sendValue;
    sendValue=0;
    SendFc6(1,1101,1,&sendValue);
}

void Device::SendFc6(unsigned char address, ushort start, ushort registers,short *value)
{
    emit Signal_ToSendFc6(address,start,registers,value);
}

void Device::SlotsInit()
{
    connect(this,SIGNAL(Signal_ToOpenDevice(QString,int)),this,SLOT(Slots_ToOpenDevice(QString,int)),Qt::BlockingQueuedConnection);
    connect(this,SIGNAL(Signal_ToSendFc6(unsigned char,ushort,ushort,short*)),this,SLOT(Slots_ToSendFc6(unsigned char,ushort,ushort,short*)),Qt::BlockingQueuedConnection);
}

void Device::Slots_ToNewDevice()
{
    modbus=new Modbus();
    modbus->port.setParity(QSerialPort::NoParity);
    modbus->port.setDataBits(QSerialPort::Data8);
    modbus->port.setStopBits(QSerialPort::OneStop);
}

void Device::Slots_ToOpenDevice(QString portName,int baudRate)
{
    qDebug()<<__FUNCTION__<<__LINE__;
    modbus->port.setPortName(portName);
    qDebug()<<__FUNCTION__<<__LINE__;
    modbus->port.setBaudRate(baudRate);
    qDebug()<<__FUNCTION__<<__LINE__;
    modbus->port.open(QSerialPort::ReadWrite);
    qDebug()<<__FUNCTION__<<__LINE__;
}

void Device::Slots_ToSendFc6(unsigned char address, ushort start, ushort registers, short *value)
{
    modbus->SendFc6(address,start,1,value);
}
